//
//  EXHeadCell.m
//  SmallEasyLife
//
//  Created by qianfeng on 15/10/25.
//  Copyright © 2015年 GXK. All rights reserved.
//

#import "EXHeadCell.h"
#import "Event.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"

@interface EXHeadCell ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;


@end

@implementation EXHeadCell

- (void)awakeFromNib {
    // Initialization code
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator= NO;
    self.scrollView.delegate = self;
    self.pageControl.currentPage = 0;
}

- (void)setModelArray:(NSArray *)modelArray
{
    _modelArray = modelArray;
    
    NSMutableArray * imageArray = [[NSMutableArray alloc] init];
    
    for (Event * evenModel in modelArray) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.scrollView addSubview:imageView];
        [imageArray addObject:imageView];
        [imageView sd_setImageWithURL:[NSURL URLWithString:evenModel.adurl]];
        
        UITapGestureRecognizer * gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTaped:)];
        [imageView addGestureRecognizer:gesture];
        
        imageView.userInteractionEnabled = YES;
        
    }
    
    for (NSInteger i = 0 ; i < imageArray.count ; ++i) {
        UIImageView * imageView = imageArray[i];
        //给imageView设置tag值
        imageView.tag = i;
        //上一张图片
        UIImageView * lastView = i==0 ? nil:imageArray[i-1];
        
        if (!lastView)
        {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.scrollView.mas_top);
                make.left.equalTo(self.scrollView.mas_left);
                make.width.equalTo(self.scrollView.mas_width);
                make.height.equalTo(self.scrollView.mas_height);
            }];
        }
        else
        {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
    
                make.top.equalTo(lastView.mas_top);
                make.left.equalTo(lastView.mas_right);
                make.width.equalTo(self.scrollView.mas_width);
                make.height.equalTo(self.scrollView.mas_height);
               
            }];
        }
    }
    
    self.pageControl.numberOfPages = self.modelArray.count;
//    CGSize size = [self.pageControl sizeForNumberOfPages:self.modelArray.count];
//    CGRect frame = self.pageControl.frame;
//    frame.size = size;
//    self.pageControl.frame = frame;
    self.pageControl.currentPage = 0;
//    NSLog(@"%f,%f",frame.origin.x,frame.origin.y);
    [self layoutIfNeeded];
    
}
- (void)imageTaped:(UIGestureRecognizer *)gesture
{
    NSLog(@"asdfafsaf");
    
    NSInteger index = gesture.view.tag;
    
    Event * eventModel = self.modelArray[index];
    
    
    [self.ownerController performSegueWithIdentifier:@"EXHeadSegue" sender:eventModel];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.scrollView.contentSize = CGSizeMake(self.contentView.frame.size.width*self.modelArray.count, 1);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
